<?php
	
	
	class Container {

		// 容器中的对象实例
		protected $instances = [];

		public function get($className)
		{

			if (isset($this->instances[$className])) {
				return call_user_func_array($this->instances[$className], []);
			}

			return null;
		}

		// 绑定对象到容器
		public function set($className, $concrete)
		{
			$this->instances[$className] = $concrete;
			return $this;
		}

	}


	class demo1 {

		public function __construct() {

		}

		public function test()
		{
			echo "hello world";
		}

	}



	// Db类
	class Db {

		public function __construct()
		{

		}

		public function select()
		{
			echo "我是查询";
		}

		public function delete()
		{
			echo "我是删除";
		}

		public function update()
		{
			echo "我是更新";
		}

	}


	$container = new Container();

	// 将demo1绑定到容器
	$container->set("demo1", function () {
		return new demo1();
	});

	// 从容器中取出demo1并调用该实例方法
	$res = $container->get("demo1")->test();


	// 将Db类绑定到容器
	$container->set("db", function () {
		return Db();
	});


	
?>